package com.example.grape_effect.animation

import android.opengl.Matrix
import com.example.grape_effect.gl.glutils.Matrix2DUtils
import com.lammy.grapeeffect.interpolator.LYInterpolator
import com.example.grape_effect.interpolator.UniformAccelerationInterpolator

open class TranslateAnimation(name1:String) : LYAnimation() {

   init {
        duration = 1500
        startTime = -1L
        isRepeatable = true
        mode = ANIMATION_MODE_BY_SET_TIME_SELF
        name = name1
        initAnimationDirection(name)
   }

    var startPosition = floatArrayOf(0f, 0f, 0f)
    var endPosition = floatArrayOf(0f, 0f, 0f)
    var curPosition = floatArrayOf(0f, 0f, 0f)
    var positionUpdaterX: LYInterpolator? = null
    var positionUpdaterY:LYInterpolator? =null
    var positionUpdaterZ:LYInterpolator? =null

    override fun updateAnimationParameters() {
        val mvpMatrix = Matrix2DUtils.getIdentityMatrix()
        if (mode == ANIMATION_MODE_BY_SET_TIME_SELF) {
            updateCurPosition(System.currentTimeMillis() - startTime)
        }
        Matrix.translateM(mvpMatrix, 0, curPosition[0],  curPosition[1],  curPosition[2])
        setMatrix(mvpMatrix)
    }

    open fun updateCurPosition(executedTime:Long) {
        var deltaTime = executedTime
        if(executedTime > duration) {
            if(isRepeatable) {
                deltaTime = executedTime % duration
            } else {
                curPosition = endPosition
                return
            }
        }
        var time = deltaTime
        positionUpdaterX?.let {
            curPosition[0] =  it.getValue(time)
        }
        positionUpdaterY?.let {
            curPosition[1] =  it.getValue(time)
        }
        positionUpdaterZ?.let {
            curPosition[2] =  it.getValue(time)
        }
    }

    companion object {
        const val leftToRight = "animation_left2Right"
        const val rightToLeft = "animation_right2Left"
        const val topToBottom = "animation_top2bottom"
        const val bottomToTop = "animation_bottom2top"
    }
    private fun initAnimationDirection(name:String){
        when(name) {
            leftToRight -> {
                startPosition = floatArrayOf(-2f,0f,0f)
                endPosition = floatArrayOf(0f,0f,0f)
                positionUpdaterX = UniformAccelerationInterpolator(duration, startPosition[0] , endPosition[0], 0.003f )
            }
            rightToLeft -> {
                startPosition = floatArrayOf(2f,0f,0f)
                endPosition = floatArrayOf(0f,0f,0f)
                positionUpdaterX = UniformAccelerationInterpolator(duration, startPosition[0] , endPosition[0], -0.003f )
            }
            topToBottom -> {
                startPosition = floatArrayOf(0f,2f,0f)
                endPosition = floatArrayOf(0f,0f,0f)
                positionUpdaterY = UniformAccelerationInterpolator(duration, startPosition[1] , endPosition[1], -0.003f )
            }
            bottomToTop -> {
                startPosition = floatArrayOf(0f,-2f,0f)
                endPosition = floatArrayOf(0f,0f,0f)
                positionUpdaterY = UniformAccelerationInterpolator(duration, startPosition[1] , endPosition[1], 0.003f )
            }
            else ->{
                startPosition = floatArrayOf(-2f,0f,0f)
                endPosition = floatArrayOf(0f,0f,0f)
                positionUpdaterX = UniformAccelerationInterpolator(duration, startPosition[0] , endPosition[0], 0.003f )
            }
        }
    }

}